package com.zhuss.core.processor;

import java.util.*;

import com.zhuss.enums.StartStopEnum;
import com.zhuss.exception.ExceptionCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.information.CreateInformationBo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.db.dao.ext.InformationDao;
import com.zhuss.core.db.entity.Information;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.DzcxException;
import com.zhuss.util.JsonUtil;


/**
 * @description: 通知公告创建
 * @author: zhushanshuo
 * @create: Tue Feb 02 15:16:28 CST 2021
 **/
@Component("informationCreate")
public class InformationCreate extends AbstractProcessor<Object, CreateInformationBo> implements Processor<Object, CreateInformationBo> {

    @Autowired
    private InformationDao informationDao;

    @Autowired
	@Qualifier("informationRead")
	private InformationRead informationRead;


    /**
     * 校验
     */
    @Override
    public void check(CreateInformationBo bo) throws DzcxException {
    	String name = bo.getTitle();
    	if (name == null || "".equals(name.trim())) {
    		 throw new DzcxException(ExceptionCode.标题为空);
    	}

		if (bo.getContent() == null || "".equals(bo.getContent().trim())) {
			throw new DzcxException(ExceptionCode.内容为空);
		}

		if (bo.getType() == null || bo.getType() <=0l) {
			throw new DzcxException(ExceptionCode.通知公告类型为空);
		}

    	List<Information> list = informationRead.list(name);
    	if (!list.isEmpty()) {
    		 throw new DzcxException(ExceptionCode.标题已存在);
    	}
    }

    /**
     * 操作方法
     */
    @Override
    public Object process(CreateInformationBo param) {
    	Information record = new Information();
    	record.setType(param.getType());
    	record.setTitle(param.getTitle());
    	record.setContent(param.getContent());
    	record.setStatus(param.getStatus() == null ? StartStopEnum.启用.key() : param.getStatus());
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	informationDao.create(record);
    	param.setTargetId(record.getInformationId());
        return null;
    }

    /**
     * 操作日志
     */
	@Override
	public Object log(CreateInformationBo param) throws DzcxException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.基础模块.key());
    	record.setAction(LogActionEnum.通知公告创建.key());
    	record.setTargetId(param.getTargetId());
    	record.setTargetName(param.getTitle());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());

    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
